﻿/*************************************************************************************
 * 文 件 名:   HK_RotTargetItemEditor
 * 
 * 描    述:   每个目标点需挂在的脚本（Inspector编辑器拓展）
 * 
 * 版    本：  V1.1
 * 创 建 者：  京产肠饭
 * 创建时间：  2021/12/8
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using UnityEngine;
using UnityEditor;

namespace HKTools
{
    [CanEditMultipleObjects]
    [CustomEditor(typeof(HK_RotTargetItem))]
    public class HK_RotTargetItemEditor : Editor
    {
        float value = 0;
        Transform camTemp_TF;

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            HK_RotTargetItem targetItem = target as HK_RotTargetItem;

            GUILayout.Space(10);

            if (GUILayout.Button("生成测试Camera"))
            {
                if (targetItem.transform.childCount > 0)
                    return;

                GameObject camTemp = new GameObject();
                camTemp.name = "Test Cam";
                camTemp.AddComponent<Camera>();

                camTemp_TF = camTemp.transform;
                camTemp_TF.SetParent(targetItem.transform);
                camTemp_TF.position = targetItem.transform.position + targetItem.transform.forward * targetItem.targetDis;
                camTemp_TF.LookAt(targetItem.transform);

                camTemp_TF.GetComponent<Camera>().fieldOfView = targetItem.FOV;

                value = (targetItem.targetDis - targetItem.minDis) / (targetItem.maxDis - targetItem.minDis);
            }

            if (GUILayout.Button("删除测试Camera"))
            {
                if (targetItem.transform.childCount <= 0)
                    return;

                DestroyImmediate(targetItem.transform.GetComponentInChildren<Camera>().gameObject);
                camTemp_TF = null;
            }

            if (camTemp_TF != null)
            {
                GUILayout.Space(5);

                value = GUILayout.HorizontalSlider(value, 0, 1);

                camTemp_TF.position = targetItem.transform.position +
                    targetItem.transform.forward * (targetItem.minDis + (targetItem.maxDis - targetItem.minDis) * value);

                GUILayout.Space(15);
            }
        }
    }
}